function [dfile, err] = readdatac_window(truefile, RL, n, mode, plot_flag)
% read some data and plot it in a separate window

global CURRENT VOLTAGE WCHAN
global CONTROL
global FILTERED_CURRENT

h2=findobj('Tag', 'DataFrame'); % now get the frame we work inside
set(h2, 'Visible', 'off');
err=0;
if(~exist('plot_flag')) % unless there's an argument at the end to indicate no plotting
    plot_flag=0;	
end;
readmode = getreadmode(nargin, n);

dfile = [];
CURRENT = [];
VOLTAGE = [];
FILTERED_CURRENT = 0;
truefile = deblank(slash4OS(truefile));
ex = exist(truefile);
if(ex ~= 2)
    QueMessage(sprintf('readdatac_window: File %s not found', truefile), 1);
    QueMessage(sprintf('  -- check that path is correct'));
    err = 1;
    return;
end;

% direct read without using the control data information....
if(~readmode)
    if(nargin<=2)
        mode = getdatamode; % just define mode here
    end;
    [dfile, err] = read_one(truefile, RL, n, 0, mode, 0);
    if(err ~= 0)
        return;
    end
    % QueMessage(sprintf('RDW: Read data %s [%d-%d] OK', dfile.filename, dfile.frec, dfile.lrec));
    % make simple plot here
    
    if(plot_flag)
        plot_data(dfile, 0);	
        % QueMessage('Plotted');
    end;
    return 
end

QueMessage(sprintf('Reading data from %s', truefile));

% Using control data information.
% first check the possibility of subtraction, and make sure the record lists are the same
% length - if not give error. 
if(strcmp(CONTROL(n).subtract, 'sub'))  % signal for subtraction
    rl1=number_arg(CONTROL(n).recbeg):number_arg(CONTROL(n).skip):number_arg(CONTROL(n).recend);
    rl2=number_arg(CONTROL(n).sub_frec):number_arg(CONTROL(n).skip):number_arg(CONTROL(n).sub_lrec);
    if(length(rl1) ~= length(rl2))
        QueMessage(sprintf('Subtraction: Can''t compute because the record lists are different lengths - %d and %d!', length(rl1), length(rl2)));
        err=1;
        return
    end
else
    rl1 = RL;
end
% ignore the passed truefile, and use the information provided by ctl
%truefile = fullfile(ctl(n).path, ctl(n).filename);
% [path, file, ext, ver] = fileparts(truefile);
% path = drive_subst(path);
% truefile = fullfile(path, ctl(n).filename);
% get file and records to read
% first determine the averaging information - we don't average single channel records.
% therefore, only VC protocols are valid
avg=0;
if(strmatch(CONTROL(n).protocol, {'iv', 'qinactp2', 'qinactp3', 'mc-qinactp2'}, 'EXACT'))
    avg=CONTROL(n).groupavg;
end;
[dfile, err] = read_one(truefile, rl1, n, 'VC', 0, avg);
if(err ~= 0)
    return;
end
if(strcmp(CONTROL(n).subtract, 'sub')) % open a second file and subtract the CURRENTs...
    nextfile = CONTROL(n).linkfile; % for the position the data arrives in...
    if(~isempty(nextfile))
        [path,name,ext,ver] = fileparts(truefile);
        t2file = strcat(path, '/');
        t2file = strcat(t2file, nextfile);
        t2file=slash4OS(t2file);
    else
            t2file = truefile;
    end;
    nrl = size(rl2);
    QueMessage(sprintf('Subtraction with %s, RL: %d-%d', t2file, rl2(1), rl2(nrl(2))));
    cs = CURRENT; % save current array temporarily
    [dfile2, err] = read_one(t2file, rl2, n, 'VC', 1, avg);
    if(err ~= 0) return;
    end
    % sizes of arrays should be appropriate, as checked above
    CURRENT = cs - CURRENT;   
end
% update information window
%QueMessage(sprintf('Read data %s [%d-%d]', dfile.filename, dfile.frec, dfile.lrec));
% QueMessage('done!');
% make simple plot here
if(plot_flag) % unless there's an argument at the end to indicate no plotting
    plot_data(dfile, n);
end;
return

